<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="150px">
      <el-form-item label="检查项细明名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入检查项细明名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="检查项明细单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入检查项明细单位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="检查项细明正常值范围中的最小值" prop="minrange">
        <el-input
          v-model="queryParams.minrange"
          placeholder="请输入检查项细明正常值范围中的最小值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查项细明正常值范围中的最大值" prop="maxrange">
        <el-input
          v-model="queryParams.maxrange"
          placeholder="请输入检查项细明正常值范围中的最大值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="检查项细明正常值" prop="normalValue">
        <el-input
          v-model="queryParams.normalValue"
          placeholder="请输入检查项细明正常值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="检查项验证范围说明文字" prop="normalValueString">
        <el-input
          v-model="queryParams.normalValueString"
          placeholder="请输入检查项验证范围说明文字"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="所属检查项编号" prop="ciId">
        <el-input
          v-model="queryParams.ciId"
          placeholder="请输入所属检查项编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="所属检查项编号" prop="ciId">
        <el-select
          v-model="queryParams.ciId"
          placeholder="请选择所属检查项编号"
          clearable
          @focus="queryTextCiId"
          @change="handleQuery"
        >
          <el-option
            v-for="item in ciOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['tijian:check_item_detailed:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tijian:check_item_detailed:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tijian:check_item_detailed:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['tijian:check_item_detailed:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="check_item_detailedList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="检查项明细编号" align="center" prop="id" />
      <el-table-column label="检查项细明名称" align="center" prop="name" />
      <el-table-column label="检查项明细单位" align="center" prop="unit" />
      <el-table-column label="检查项细明正常值范围中的最小值" align="center" prop="minrange" />
      <el-table-column label="检查项细明正常值范围中的最大值" align="center" prop="maxrange" />
      <el-table-column label="检查项细明正常值" align="center" prop="normalValue" />
      <el-table-column label="检查项验证范围说明文字" align="center" prop="normalValueString" />
      <el-table-column label="检查项明细类型" align="center" prop="type" />
      <el-table-column label="所属检查项编号" align="center" prop="ciId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['tijian:check_item_detailed:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['tijian:check_item_detailed:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改检查项明细对话框 -->
    <el-dialog :title="title" v-model="open" width="730px"  append-to-body>
      <el-form ref="check_item_detailedRef" :model="form" :rules="rules" label-width="200px">
        <el-form-item label="检查项细明名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入检查项细明名称" />
        </el-form-item>
        <el-form-item label="检查项明细单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入检查项明细单位" />
        </el-form-item>
        <el-form-item label="正常值范围中的最小值" prop="minrange">
          <el-input v-model="form.minrange" placeholder="请输入检查项细明正常值范围中的最小值" />
        </el-form-item>
        <el-form-item label="正常值范围中的最大值" prop="maxrange">
          <el-input v-model="form.maxrange" placeholder="请输入检查项细明正常值范围中的最大值" />
        </el-form-item>
        <el-form-item label="检查项细明正常值" prop="normalValue">
          <el-input v-model="form.normalValue" placeholder="请输入检查项细明正常值" />
        </el-form-item>
        <el-form-item label="检查项验证范围说明文字" prop="normalValueString">
          <el-input v-model="form.normalValueString" placeholder="请输入检查项验证范围说明文字" />
        </el-form-item>
        <el-form-item label="所属检查项编号" prop="ciId">
          <el-input v-model="form.ciId" placeholder="请输入所属检查项编号" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Check_item_detailed">
import { listCheck_item_detailed, getCheck_item_detailed, delCheck_item_detailed, addCheck_item_detailed, updateCheck_item_detailed, listCheck_item_detailed_CiIdAndName} from "@/api/tijian/check_item_detailed";
import { ref, reactive } from 'vue';

const { proxy } = getCurrentInstance();

const check_item_detailedList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const ciOptions = ref([]);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null,
    name: null,
    unit: null,
    minrange: null,
    maxrange: null,
    normalValue: null,
    normalValueString: null,
    type: null,
    ciId: null,
  },
  rules: {
    name: [
      { required: true, message: "检查项细明名称不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "检查项明细类型不能为空", trigger: "change" }
    ],
    ciId: [
      { required: true, message: "所属检查项编号不能为空", trigger: "blur" }
    ],
  },
  queryText:{
    ciId:null,
    ciName:null
  }
});

const { queryParams, form, rules, queryText } = toRefs(data);



/** 查询检查项明细列表 */
function getList() {
  loading.value = true;
  listCheck_item_detailed(queryParams.value).then(response => {
    check_item_detailedList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    unit: null,
    minrange: null,
    maxrange: null,
    normalValue: null,
    normalValueString: null,
    type: null,
    ciId: null,
    remark: null
  };
  proxy.resetForm("check_item_detailedRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加检查项明细";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getCheck_item_detailed(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改检查项明细";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["check_item_detailedRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCheck_item_detailed(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCheck_item_detailed(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除检查项明细编号为"' + _ids + '"的数据项？').then(function() {
    return delCheck_item_detailed(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('tijian/check_item_detailed/export', {
    ...queryParams.value
  }, `check_item_detailed_${new Date().getTime()}.xlsx`)
}

/** ciId下拉框操展示 */
function queryTextCiId() {
    loading.value = true;
    listCheck_item_detailed_CiIdAndName().then(response => {
        // 直接使用响应中的rows属性
        ciOptions.value = response.rows.map(item => ({
            id: item.id,
            name: item.ciName
        }));
        loading.value = false;
    }).catch(error => {
        console.error("Error fetching ciId list:", error);
        loading.value = false;
    });
}

getList();
loading.value = false;
</script>


<style scoped>
  /* 让 label 文字垂直、水平都对齐，且高度统一 */
 .el-form-item__label {
    line-height: 30px;
    vertical-align: top;
    padding: 5px 0; /* 上下添加相同的内边距，保持高度一致 */
    text-align: left ; /* 让文字靠右对齐，可根据实际需求调整为 left 或 center */
        width: 200px;
        min-height: 32px;
  }


  /* 表格表头列单元格的内边距 */
 .el-table__header-wrapper th.el-table-column {
    padding-bottom: 20px;
    padding-top: 20px;
  }

  /* 表格表体列单元格的内边距 */
 .el-table__body-wrapper td.el-table-column {
    padding-bottom: 20px;
    padding-top: 20px;
  }

  /* 表单 item 的上下内边距 */
  .el-form-item {
        display: flex;
        align-items: baseline;
    }

    /* label部分样式 */
   .el-form-item__label {
        text-align: left;
        width: 200px;
        min-height: 32px;
        line-height: 32px;
    }


</style>